327D - Block Tower - CodeForces Solution


constructive algorithms dfs and similar graphs *1900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>  
#include <ext/pb_ds/tree_policy.hpp> 
#include <ctime>
// #include <boost/multiprecision/cpp_int.hpp>  
using namespace std;
 using namespace __gnu_pbds;
 // using namespace boost::multiprecision;

// // #pragma GCC target ("avx2")
// // #pragma GCC optimization ("O3")
// // #pragma GCC optimization ("unroll-loops")
 template <typename T>
 using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
 template <typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>; 
 
// DEFINE STATEMENTS
const long long inf = 1e18;
// #define M 1000000007
#define int ll
// #define lul int128_t
#define fr(i,a,n) for(ll i=a;i<n;i++)
#define frn(i,a,n) for(ll i=a; i>=n; i--)
#define repit(x) for(auto it=x.begin();it!=x.end();it++)
#define repitr(x) for(auto it=x.rbegin();it!=x.rend();it++)
// #define size(x) (int)(x.size())
#define pb push_back
#define pob pop_back
#define mp make_pair
#define f first
#define s second
#define fix(f,n) std::fixed<<std::setprecision(n)<<f
#define all(x) x.begin(), x.end()
#define M_PI 3.14159265358979323846
#define epsilon (double)(0.000000001)
#define popcount __builtin_popcountll
#define fileio(x,y) freopen(x, "r", stdin); freopen(y, "w", stdout);
#define lc(i) (i<<1)
#define rc(i) (lc(i)|1)
#define mid(x,y) (y-x)/2+x
#define ll1(p) ll p;cin>>p
#define ll2(p,q) ll p,q;cin>>p>>q
#define ll3(p,q,r) ll p,q,r;cin>>p>>q>>r
#define ll4(p,q,r,s) ll p,q,r,s;cin>>p>>q>>r>>s
#define ch1(p) char p;cin>>p
#define ch2(p,q) char p,q;cin>>p>>q
#define str1(p) string p;cin>>p
#define str2(p,q) string p,q;cin>>p>>q
 
typedef long long ll;
typedef vector<long long> vll;
typedef pair<long long, long long> pll;
typedef vector<pair<long long, long long>> vpll;
typedef vector<int> vi;
 
// DEBUG FUNCTIONS 
#ifdef LOCALFLAG
template<typename T>
void __p(T a) { cout<<a; }
template<typename T, typename F>
void __p(pair<T, F> a) { cout<<"{"; __p(a.first); cout<<","; __p(a.second); cout<<"}"; }
template<typename T>
void __p(std::vector<T> a) { cout<<"{"; for(auto it=a.begin(); it<a.end(); it++) __p(*it),cout<<",}"[it+1==a.end()]; }
template<typename T>
void __p(std::set<T> a) { cout<<"{"; for(auto it=a.begin(); it!=a.end();){ __p(*it); cout<<",}"[++it==a.end()]; } }
template<typename T>
void __p(std::multiset<T> a) { cout<<"{"; for(auto it=a.begin(); it!=a.end();) { __p(*it); cout<<",}"[++it==a.end()]; } }
template<typename T, typename F>
void __p(std::map<T,F> a) { cout<<"{\n"; for(auto it=a.begin(); it!=a.end();++it) { __p(it->first); cout << ": "; __p(it->second); cout<<"\n"; } cout << "}\n"; }
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) { __p(a1); __p(a...); }
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) { cout<<name<<" : "; __p(arg1); cout<<endl; }
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) { int bracket=0,i=0; for(;; i++) if(names[i]==','&&bracket==0) break; else if(names[i]=='(') bracket++; else if(names[i]==')') bracket--; const char *comma=names+i; cout.write(names,comma-names)<<" : "; __p(arg1); cout<<" | "; __f(comma+1,args...); }
#define trace(...) cout<<"Line:"<<__LINE__<<" ", __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...)
#define error(...)
#endif
// DEBUG FUNCTIONS END 


struct move1{
    char color=0;
    int x=0; 
    int y=0;
};
vector<move1> ans;
vector<vector<bool>> vis;
vector<vector<int>> moves = {{-1,0},{1,0},{0,1},{0,-1}} ;
vector<pair<int,int>> curr_pos;
int init_x=0;
int init_y=0;

void dfs(int i,int j,int n, int m){
    vis[i][j]=true;
    // trace(i,j);
    curr_pos.pb({i,j});
    move1 temp;
    temp.color = 'B';
    temp.x = i;
    temp.y = j;
    ans.pb(temp);
    trace("entry");
    for(vector<int> v : moves){
        // trace(v);
        int x = i+v[0];
        int y = j+v[1];
        if(x>-1 && x<n && y>-1 && y<m){
            if(!vis[x][y]){
                dfs(x,y,n,m);
                trace("hello");
                move1 temp2;
                temp2.color = 'D';
                temp2.x = x;
                temp2.y = y;
                ans.pb(temp2);
                temp2.color = 'R';
                ans.pb(temp2);
            }
        } 
    }
    trace("bye");
}




signed main(){
    #ifdef LOCALFLAG 
        freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    #endif
    //fileio(".in",".out")
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    // int t; cin >> t;
    int t=1;
    while(t--){
        int n,m; cin >> n>> m;
        vis.resize(n);
        fr(i,0,n)vis[i].resize(m);
        // trace(vis);
        // vector<vector<int>> v(n+1);
        fr(i,0,n){
            string st; cin >> st;
            fr(j,0,m){
                if(st[j]=='#')vis[i][j]=true;
            }
        }
        fr(i,0,n){
            // trace(vis);
            fr(j,0,m){
// cout << 1<< endl;
                if(!vis[i][j]){
                    init_x= i; init_y=j;
                    dfs(i,j,n,m);
                }
            }
        }
        cout << ans.size()<< "\n";
        fr(i,0,ans.size()){
            cout << ans[i].color <<" "<< ans[i].x+1 << " "<< ans[i].y+1<< "\n";
        }
    }
}


Comments

Submit
0 Comments
More Questions

981A - Antipalindrome
365A - Good Number
1204B - Mislove Has Lost an Array
1409D - Decrease the Sum of Digits
1476E - Pattern Matching
1107A - Digits Sequence Dividing
1348A - Phoenix and Balance
1343B - Balanced Array
1186A - Vus the Cossack and a Contest
1494A - ABC String
1606A - AB Balance
1658C - Shinju and the Lost Permutation
1547C - Pair Programming
550A - Two Substrings
797B - Odd sum
1093A - Dice Rolling
1360B - Honest Coach
1399C - Boats Competition
1609C - Complex Market Analysis
1657E - Star MST
1143B - Nirvana
1285A - Mezo Playing Zoma
919B - Perfect Number
894A - QAQ
1551A - Polycarp and Coins
313A - Ilya and Bank Account
1469A - Regular Bracket Sequence
919C - Seat Arrangements
1634A - Reverse and Concatenate
1619C - Wrong Addition